package com.vanysoft.business.demo.table.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.demo.table.dao.TDemoTableMapper;
import com.vanysoft.business.demo.table.entity.TDemoTable;
import com.vanysoft.business.demo.tableCols.dao.TableColsMapper;
import com.vanysoft.framework.exception.VanySoftBaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名称：DemoFormService
 * 类描述：DEMO列表逻辑层类
 * 创建人：SharpClaw_X
 * 创建时间：2017年10月21日下午7:33:20
 * 修改人：SharpClaw_X
 * 修改时间：2017年10月21日
 * 版本信息：@version 1.0
 * Copyright (c) 2017 SharpClaw
 * 修改备注：
 */
@Service
@Transactional(readOnly = true)
public class DemoTableService {

    @Autowired
    private TDemoTableMapper tDemoTableMapper;

    @Autowired
    private TableColsMapper tableColsMapper;

    /**
     * Function Name               loginValidate
     *
     * @param dataType
     * @return
     * @throws VanySoftBaseException
     * @description 根据数据类型返回对应的数据
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日下午12:07:23          SharpClaw_X          Initial
     * *********************************************************************
     */
    public Map<String, Object> queryDemoTable(Integer pageSize, Integer pageNum, String dataType, String searchValue) throws VanySoftBaseException {
        Map<String, Object> demoTables = new HashMap<String, Object>();
        //分页工具
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        List<Map<String, Object>> list = tDemoTableMapper.selectByDataTypeAndSearchValue(page, searchValue, dataType);
        demoTables.put("rows", list);
        demoTables.put("total", page.getTotal());
        return demoTables;
    }

    /**
     * Function Name               addDemoTable
     *
     * @return
     * @throws VanySoftBaseException
     * @description 新增记录
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日下午12:07:23          SharpClaw_X          Initial
     * *********************************************************************
     */
    @Transactional
    public void addDemoTable(TDemoTable obj) throws VanySoftBaseException {
        tDemoTableMapper.insert(obj);
    }

    /**
     * Function Name               updateDemoTable
     *
     * @return
     * @throws VanySoftBaseException
     * @description 修改记录
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日下午12:07:23          SharpClaw_X          Initial
     * *********************************************************************
     */
    @Transactional
    public void updateDemoTable(TDemoTable obj) throws VanySoftBaseException {
        tDemoTableMapper.updateByPrimaryKey(obj);
    }

    /**
     * Function Name               deleteDemoTable
     *
     * @return
     * @throws VanySoftBaseException
     * @description 删除记录
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日下午12:07:23          SharpClaw_X          Initial
     * *********************************************************************
     */
    @Transactional
    public void deleteDemoTable(String demoTableKey) throws VanySoftBaseException {
        tDemoTableMapper.deleteByPrimaryKey(demoTableKey);
    }

    /**
     * Function Name               getTableCols
     *
     * @param tablename
     * @return
     * @throws VanySoftBaseException
     * @description 获取表的列信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月22日下午12:07:23          SharpClaw_X          Initial
     * *********************************************************************
     */
    @Transactional
    public List<Map<String, Object>> getTableCols(String tablename) throws VanySoftBaseException {
        return tableColsMapper.selectByTablename(tablename);
    }
}
